using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Queries;
using NHibernate.Expression;

/// <summary>
/// Summary description for Users
/// </summary>
/// 
[ActiveRecord]
public class Users:ActiveRecordBase<Users>
{
    public static  MatchMode Exact = MatchMode.Exact;
    public static   MatchMode Anywhere = MatchMode.Anywhere;
    public static  MatchMode End = MatchMode.End;
    public static  MatchMode Start = MatchMode.Start;   

    private int userID;  

    [PrimaryKey(PrimaryKeyType.Identity)]
    public int UserID
    {
        get { return userID; }
        set { userID = value; }
    }

    private String userName;

    [Property(Unique=true)]
    public String UserName
    {
        get { return userName; }
        set { userName = value; }
    }

    private String password;

    [Property]
    public String Password
    {
        get { return password; }
        set { password = value; }
    }

    private String email;
    [Property]
    public String Email
    {
        get { return email; }
        set { email = value; }
    }

    private String firstName;
    [Property]
    public String FirstName
    {
        get { return firstName; }
        set { firstName = value; }
    }

    private String lastName;
    [Property]
    public String LastName
    {
        get { return lastName; }
        set { lastName = value; }
    }

    private String mobiPhone;
    [Property]
    public String MobiPhone
    {
        get { return mobiPhone; }
        set { mobiPhone = value; }
    }

    private bool activeFlag;
    [Property]
    public bool ActiveFlag
    {
        get { return activeFlag; }
        set { activeFlag = value; }
    }

    private DateTime createdDate;
    [Property]
    public DateTime CreatedDate
    {
        get { return createdDate; }
        set { createdDate = value; }
    }

    private DateTime updatedDate;
    [Property]
    public DateTime UpdatedDate
    {
        get { return updatedDate; }
        set { updatedDate = value; }
    }

    private String createdBy;
    [Property]
    public String CreatedBy
    {
        get { return createdBy; }
        set { createdBy = value; }
    }


    private String updatedBy;
    [Property]
    public String UpdatedBy
    {
        get { return updatedBy; }
        set { updatedBy = value; }
    }

	public Users()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public Users(String userName,String password,String  email,String firstName,String lastName,String mobiPhone,bool activeFlag,String createdBy )
    {
        this.userName = userName;
        this.password = password;
        this.email = email;
        this.firstName = firstName;
        this.lastName = lastName;
        this.mobiPhone = mobiPhone;
        this.activeFlag = activeFlag;
        this.createdDate = DateTime.Now;
        this.updatedDate = DateTime.Now;
        this.createdBy = createdBy;
        this.updatedBy = createdBy;        
    }

    public static void delete_user(int userid)
    {
        Users user = get_user(userid);
        user.Delete();
    }

    public static void update_user(int userID, String userName, String password, String email, String firstName, String lastName, String mobiPhone, bool activeFlag, String updatedBy)
    {
        Users user = new Users();
        user = get_user(userID);
        user.userName = userName;
        user.password = password;
        user.email = email;
        user.firstName = firstName;
        user.lastName = lastName;
        user.mobiPhone = mobiPhone;
        user.activeFlag = activeFlag;
        user.UpdatedBy = updatedBy;
        user.updatedDate = DateTime.Now;
        user.Update();
    }

    public static void update_user(int userID,bool activeFlag,String updatedBy)
    {
        Users user = new Users();
        user = get_user(userID);        
        user.activeFlag = activeFlag;
        user.UpdatedBy = updatedBy;
        user.updatedDate = DateTime.Now;
        user.Update();
    }

    public static void create_user(String userName, String password, String email, String firstName, String lastName, String mobiPhone, bool activeFlag,String createdBy)
    {
        Users user = new Users(userName,password,email,firstName,lastName,mobiPhone,activeFlag,createdBy);
        user.Create();
        
    }

    public static Users get_user(String username)
    {
        try
        {
            Users[] users = FindAllByProperty("UserName", username);
            return users[0];
        }
        catch (Exception ex)
        {
            return null;
        }
    }

    public static Users get_user(int userid)
    {
        return FindByPrimaryKey(userid);
    }

    public static Users[] get_all_user()
    {
        return FindAll();
    }

    public static int count_all_user()
    {
        return Count();
    }

    public static Users[] search_user(String propertyName,String textSearch,MatchMode mode)
    {
        DetachedCriteria criteria = DetachedCriteria.For<Users>();
        criteria.Add(Expression.InsensitiveLike(propertyName, textSearch, mode));
        return FindAll(criteria, Users.Asc("UserID"));
    }

    public static Users[] get_users_and_sort(String propertyName, String order)
    {
        DetachedCriteria avgWeight = DetachedCriteria.For<Users>();
        if (order == "ASC")
            return FindAll(avgWeight, Asc(propertyName));
        return FindAll(avgWeight, Desc(propertyName));
    }
}
